﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProgramMain.Database;

namespace ProgramMain.OpticNet
{
    public partial class VertexCaptionDetail : ProgramMain.Framework.BaseModalDetailCtl
    {
        public VertexCaptionDetail()
        {
            InitializeComponent();
            pictureVertex.BackColor = Color.FromArgb(100, layoutPanelControl.BackColor);
        }
        private Point center = Point.Empty;
        private Point startPoint = Point.Empty;
        private Size CaptionPosition = System.Drawing.Size.Empty;

        protected override void InitializeDataControls()
        {
            center = new Point(layoutPanelControl.Size.Width / 2, layoutPanelControl.Size.Height / 2);
            pictureVertex.Location = center - new Size(12, 12);
            if (center != Point.Empty)
            {
                vertexCaption.Location = center + CaptionPosition;
            }
        }

        protected override void BindDataControls()
        {
            this.vertexCaption.DataBindings.Add(new Binding("Text", this.dataSource, "Caption"));
            try
            {
                SimpleOpticNetDB.VertexesRow row = (SimpleOpticNetDB.VertexesRow) ((DataRowView) dataSource.Current).Row;
                CaptionPosition = new Size(row.CaptionX, row.CaptionY);

                if (row.IsIsEmptyNull() || row.IsEmpty)
                {
                    // BitmapVertex = Properties.Resources.transitvertex_24;
                    pictureVertex.Image = Properties.Resources.transitvertex_24;
                }
                else
                {
                    if (!row.IsImageIndexNull())
                        // BitmapVertex = Properties.Resources.office_24;
                        pictureVertex.Image = Properties.Resources.office_24;
                    else
                    {
                        // BitmapVertex = Properties.Resources.vertex_24;
                        pictureVertex.Image = Properties.Resources.vertex_24;
                    }
                }
            }
            catch (Exception)
            {
                CaptionPosition = new Size(15, 15);
            }
        }

        private void vertexCaption_MouseDown(object sender, MouseEventArgs e)
        {
            startPoint = e.Location;
        }

        private void vertexCaption_MouseMove(object sender, MouseEventArgs e)
        {
            if (startPoint != Point.Empty)
            {
                System.Drawing.Size shift = new Size(e.X - startPoint.X, e.Y-startPoint.Y );
                vertexCaption.Location = vertexCaption.Location + shift;
            }
        }

        private void vertexCaption_MouseUp(object sender, MouseEventArgs e)
        {
            startPoint = Point.Empty;
            try
            {
                SimpleOpticNetDB.VertexesRow row = (SimpleOpticNetDB.VertexesRow)((DataRowView)dataSource.Current).Row;
                row.CaptionX = vertexCaption.Location.X - center.X;
                row.CaptionY = vertexCaption.Location.Y - center.Y;
                dataSource.EndEdit();
            }
            catch (Exception)
            {
               
            }
        }
    }
}
